library(tidyverse)
library(fixest)
library(showtext)
library(vtable)
library(writexl)
library(haven)
library(gridExtra)

setwd('D:\\WorkingPaper-Series\\Township_Light\\Replication_Files\\workdata')

#################################
########## Figure 4 #############
#################################

robust <- read_delim('robustness_plot.csv')
robust <- robust %>% mutate(upper_ci = coef + 1.96*std_err, lower_ci = coef - 1.96*std_err)

content <- robust %>% distinct(content)
content <- unlist(as.list(content$content))


robust %>% ggplot(aes(check,coef, shape = outcome, color = outcome))+
  geom_hline(yintercept = 0, color = 'grey50',lty =3)+
  geom_vline(xintercept = 2,color = 'grey50',lty =1)+
  geom_vline(xintercept = 7,color = 'grey50',lty =2)+
  geom_vline(xintercept = 11,color = 'grey50',lty =2)+
  geom_vline(xintercept = 14,color = 'grey50',lty =2)+
  geom_vline(xintercept = 17,color = 'grey50',lty =2)+
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
                width = 0, lwd = .5,position=position_dodge(0.5))+
  geom_point(aes(fill = outcome),position=position_dodge(0.5))+
  theme_bw()+
  scale_x_continuous(trans = 'reverse',breaks = c(1,3,4,5,6,8,9,10,12,13,15,16,18),labels = content)+
  scale_y_continuous(breaks = seq(-0.06,0.02,0.02))+
  coord_flip(xlim = c(18,1),ylim = c(-0.07,0.02))+
  theme(legend.position = c(0.87,0.9),
        legend.background = element_rect(color = 'black', size = 0.1,linewidth = 0.1, fill = 'white'),
        legend.margin = margin(t = -0.4, r = 0.1, b = 0.1, l = 0.1, unit = "cm"),
        text = element_text(family = 'serif',size = 9),
        axis.text =  element_text(family = 'serif',size = 9))+
  labs(y = 'Coefficient', x = '')+
  scale_color_manual(values = c('log_light' = 'black', 'ihs_light' = 'grey70'),
                     labels = c('log_light' = 'Log(light+1)', 'ihs_light' = 'IHS(light)')) +
  scale_fill_manual(values = c('log_light' = 'black', 'ihs_light' = 'grey70'),
                    labels = c('log_light' = 'Log(light+1)', 'ihs_light' = 'IHS(light)')) +
  scale_shape_manual(values = c('log_light' = 21, 'ihs_light' = 24),
                     labels = c('log_light' = 'Log(light+1)', 'ihs_light' = 'IHS(light)')) +
  guides(colour = guide_legend(""),fill = guide_legend(""),shape = guide_legend(""))
